package com.sean.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.sean.order.service.IOrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private IOrderService orderService;

    @GetMapping("/add")
    // @SentinelResource(value = "add", blockHandler = "flowBlockHandler")
    public String add(){
        System.out.println("OrderController: 下单成功");
        return "生成订单";
    }

    @GetMapping("/get")
    public String get() throws InterruptedException{
        return "查询订单";
    }

    public String flowBlockHandler(BlockException blockException){
        return "流控";
    }

    @GetMapping("/flowThread")
//    @SentinelResource(value = "flowThread", blockHandler = "flowBlockHandler")
    public String flowThread() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        System.out.println("OrderController: 下单成功");
        return "OrderController:flowThread";
    }

    @GetMapping("/flowError")
    public String flowError() throws Exception{
        int i = 1 / 0;
        return "OrderController:flowError";
    }

    @GetMapping("/test1")
    public String test1(){
        return orderService.getUser();
    }


    @GetMapping("/test2")
    public String test2(){
        return orderService.getUser();
    }


    @GetMapping("/get/{id}")
    @SentinelResource(value = "getById", blockHandler = "hotBlockHandler")
    public String getById(@PathVariable("id") Integer id) throws Exception{
        return "Order:get 正常访问";
    }

    public String hotBlockHandler(@PathVariable("id") Integer id, BlockException blockException) throws Exception{
        return "热点异常处理";
    }
}
